﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
using System.IO;
namespace _5ChickDeal.Admin
{
    public partial class DeleteDeal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.QueryString["d"] == null)
            {
                return;
            }
            int id = -1;
            try
            {
                id = int.Parse(Request.QueryString["d"]);
            }
            catch
            {
                return;
            }

            DataClassesDataContext db = new DataClassesDataContext();
            try
            {
                var deal = db.Deals.Single(d => d.id == id);


                var binhluan = deal.BinhLuans;

                var hinhanh = deal.HinhAnhs;

                foreach (HinhAnh h in hinhanh)
                {
                    File.Delete(Server.MapPath("~/Resource/ImageDeals/") + h.ten);   
                }


                db.BinhLuans.DeleteAllOnSubmit(binhluan);
                db.HinhAnhs.DeleteAllOnSubmit(hinhanh);
                db.SubmitChanges();



                db.Deals.DeleteOnSubmit(deal);
                db.SubmitChanges();
                
                
            }
            catch(Exception ex)
            {
                Response.Redirect("~/Admin/DealControl.aspx?error=true");
                return;
            }
            Response.Redirect("~/Admin/DealControl.aspx?error=false");
        }
    }
}
